package com.hamster.crm.web;

import com.hamster.crm.exception.ExceptionEnum;
import com.hamster.crm.exception.LyException;
import com.hamster.crm.pojo.ManagerGrade;
import com.hamster.crm.service.ManagerGradeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * （服务部、事业部）经理级别设置的controller
 * @author Tarry
 * @create 2019/12/20 16:31
 */
@RestController
@RequestMapping("managerGrade")
public class ManagerGradeController {
    @Autowired
    private ManagerGradeService managerGradeService;

    /**
     * 新增一条经理级别设置
     * @param managerGrade
     * @return 状态码 200
     */
    @PostMapping("insert")
    public ResponseEntity<Void> insert(@RequestBody ManagerGrade managerGrade){
//        服务部经理department=2，事业部department=3。由前台设置好
        managerGradeService.insert(managerGrade);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据 id 删除 经理级别设置记录
     * @param ids
     * @return 状态码 200
      */
    @PostMapping("delete")
    public ResponseEntity<Void> delete(@RequestBody List<String> ids){
        managerGradeService.delete(ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id查询指定的 经理级别记录
     * @return managerGrade 一条记录
     */
    @GetMapping("get")
    public ResponseEntity<ManagerGrade> getManagerGradeById(@RequestParam String id){
        ManagerGrade managerGrade = managerGradeService.getManagerGradeById(id);
        return ResponseEntity.ok(managerGrade);
    }

    /**
     * 修改一条 经理级别设置 记录
     * @param managerGrade
     * @return 状态码 200
     */
    @PostMapping("update")
    public ResponseEntity<Void> updateManagerGrade(@RequestBody ManagerGrade managerGrade){
        managerGradeService.updateManagerGrade(managerGrade);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取 经理级别设置 列表(根据department参数，区分获取 2服务部、 3事业部)
     * @return List<ManagerGrade> 对应部门的list列表， 状态码200
     *       如果异常 状态码 400 ，响应体格式如下:
     *       {
     *          "status": 400,
     *          "message": "部门参数不能为空！",
     *          "timestamp": 1576834330646
     *       }
     */
    @GetMapping("list")
    public ResponseEntity<List<ManagerGrade>> listManagerGrades(@RequestParam(required = false) String department){
        //如果参数为空，抛出自定义异常
        if (StringUtils.isBlank(department)){
            throw new LyException(ExceptionEnum.DEPARTMENTPARAM_IS_NOT_NULL);
        }
        List<ManagerGrade> list = managerGradeService.listManagerGrades(department);
        return ResponseEntity.ok(list);
    }

}
